package easymall.controller.admin;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import easymall.clients.CategoryService;
import easymall.clients.ProductsService;
import easymall.pojo.MyProducts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import easymall.po.Category;
import easymall.po.Products;
import easymall.pojo.ResponseMessage;

//商品管理
@Controller("productsControllerAdmin")
public class ProductsControllerAdmin extends AdminFilterController{
//public class ProductsControllerAdmin {
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ProductsService productsService;

	/************************* 添加商品信息 *********************/
	@RequestMapping("/admin/addprod")
	public String addprod(HttpServletRequest request, Model model) {
		// 查找商品表中所有的商品类别
		List<Category> categorys = categoryService.allcategorys();
		model.addAttribute("categorys", categorys);
		model.addAttribute("myproducts", new MyProducts());

		// 添加一条消息
		String msg = request.getParameter("msg");
		if (msg != null) {
			model.addAttribute("msg", msg);
		}
		System.out.println(msg);
		return "admin/add_prod";
	}

	@RequestMapping("/admin/save")
	public String save(@Valid @ModelAttribute("myproducts") MyProducts myproducts, BindingResult result, Model model,
					   HttpServletRequest request) throws UnsupportedEncodingException {

		List<Category> categorys = categoryService.allcategorys();
		model.addAttribute("categorys", categorys);

		// 验证
		if (myproducts.getImgurl().getSize() == 0) {
			model.addAttribute("imgError", "请上传图片");
		}
		if (result.hasErrors() || myproducts.getImgurl().getSize() == 0) {
			return "admin/add_prod";
		}

		// 保存商品信息
		String msg = productsService.save(myproducts);
		model.addAttribute("msg", msg);
		if (msg.equals("商品添加成功")) { // 添加成功后不需要model里的数据了，直接跳转到addprod路由，防止重复提交
			return "redirect:/admin/addprod?msg=" + URLEncoder.encode(msg, "UTF-8");
		}
		return "admin/add_prod";
	}

	/************************** 显示商品信息 ****************************/

	// 显示商品列表
	@RequestMapping("/admin/prodlist")
	public String prodList(Integer page, Model model) {
		int totalPage = productsService.getTotalPageByProducts();
		if (page == null || page<1)
			page = 1;
		if (page > totalPage)
			page = totalPage;
		List<Products> products = productsService.getProductsByPage(page);
		model.addAttribute("products", products);
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("page", page);

		return "admin/prod_list";
	}

	// 删除商品
	@RequestMapping("/admin/delproduct")
	@ResponseBody
	public ResponseMessage delProduct(String id) {
		ResponseMessage res = new ResponseMessage();
		res.setCode(productsService.delProduct(id));
		return res;
	}

	// 编辑商品
	@RequestMapping("/admin/editprod")
	public String editProduct(String id, Model model) {
		// 查找商品表中所有的商品类别
		List<Category> categorys = categoryService.allcategorys();
		model.addAttribute("categorys", categorys);
		Products products;
		if (id == null) { // 新增
			products = new Products();
		} else { // 修改
			products = productsService.oneProduct(id);
			model.addAttribute("id", id);
		}

		model.addAttribute("myproducts", products);
		model.addAttribute("imgurl", products.getImgurl());
		return "admin/edit_prod";
	}

	// 保存修改
	@RequestMapping("/admin/saveEditprod")
	public String saveEditprod(@Valid @ModelAttribute("myproducts") MyProducts myproducts, BindingResult result,
			Model model, HttpServletRequest request, String id) throws UnsupportedEncodingException {

		List<Category> categorys = categoryService.allcategorys();
		model.addAttribute("categorys", categorys);
		model.addAttribute("id", id); // id要回填
		// 图片要额外获取
		String imgurl = productsService.oneProduct(id).getImgurl();
		model.addAttribute("imgurl", imgurl);

		// 验证
//		if (myproducts.getImgurl().getSize() == 0) {
//			model.addAttribute("imgError", "请上传图片");
//		}
		if (result.hasErrors()) {
			return "admin/edit_prod";
		}

		// 保存商品信息
		String msg = productsService.updatePruduct(myproducts, id);
		model.addAttribute("msg", msg);
		
		//再次获取一次，因为图片可能更新
		imgurl = productsService.oneProduct(id).getImgurl();
		model.addAttribute("imgurl", imgurl);

		return "admin/edit_prod";
	}

}
